var dbConfig = require('../utils/dbconfig');

getRightsList = (req,res)=>{

    var sql = "select * from rights_info "
    dbConfig.sqlConnect(sql,[],(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                code:200,
                data:data
            })
        }
    });
}


let df = (data)=>{
    var arr=[];
    let role_arr=[];

    var totalobj = {};

    for(var i=0;i<data.length;i++){
        var role_id = data[i].role_id;
        var obj = arr.find(function(role_id_obj){
            return role_id_obj.role_id == role_id
        });
        if(obj == null){
            var oo = {
                'role_id':data[i].role_id,
                'role_name':data[i].role_name,
                'role_desc':data[i].role_desc,
                'child':[{
                    'rights_id':data[i].rights_id,
                    'rights_name':data[i].rights_name,
                    'child':[]
                }]
            }
            arr.push(oo)
            totalobj[data[i].rights_id] = [arr.length-1,0]
        }else{
            if(data[i].prights_id == null){
                var level0 = arr.findIndex((role_id_obj)=> role_id_obj.role_id == role_id);
                arr[level0].child.push({
                    'rights_id':data[i].rights_id,
                    'rights_name':data[i].rights_name,
                    'child':[]
                })
                totalobj[data[i].rights_id] = [0,arr[level0].child.length-1]
            }else if(data[i].rights_level == '1'){
                var rid = data[i].role_id;
                var pid = data[i].prights_id;
                //寻找第一级节点
                var index0 = arr.findIndex((item)=> item.role_id == rid);
                var index1 = arr[index0].child.findIndex((item)=>item.rights_id == pid);
                var c0 = arr[index0].child
                c0[index1].child.push({
                    'rights_id':data[i].rights_id,
                    'rights_name':data[i].rights_name,
                    'child':[]
                })
                arr[index0].child = c0
                // totalobj[data[i].rights_id] = [index1,c0.length-1]
                var gg = [];
                var tt = totalobj[pid];
                gg = gg.concat(tt);
                var ll = c0[index1].child.length-1;
                gg.push(ll);
                var v = data[i].rights_id;
                totalobj[v] = gg;
            }else if(data[i].rights_level == '2'){
                // console.log(totalobj)
                let rid = data[i].role_id;
                let pid = data[i].prights_id;
                //寻找第一级节点
                let index0 = arr.findIndex((item)=> item.role_id == rid);
                let obj0 = arr.find((item)=> item.role_id == rid);
                let indexarr = totalobj[pid];
                if(indexarr != null){
                    let obj1 = obj0.child;
                    let obj2 = obj1[indexarr[1]].child;
                    let obj3 = obj2[indexarr[2]].child;
                    obj3.push({
                        'rights_id':data[i].rights_id,
                        'rights_name':data[i].rights_name
                    })
                    obj2[indexarr[2]].child = obj3;
                    obj1[indexarr[1]].child = obj2;
                    obj0.child = obj1;
                    arr[index0] = obj0;
                }
            }

        }
    }
    return arr
}


getRoleList = (req,res)=>{
    var sql = "select t1.*,t3.* from role_info t1 left join role_right_rel t2 on t1.role_id = t2.role_id  left join rights_info t3 on t2.rights_id = t3.rights_id order by t1.role_id,t3.rights_level,t3.prights_id"
    dbConfig.sqlConnect(sql,[],(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            console.log('===============================')
            res.send({
                code:200,
                data:df(data)
            })
        }
    });
}

allowUserRole = (req,res)=>{
    let { user_id, role_id } = req.body
    dbConfig.sqlConnect("update user_info set role_id =? where user_id=?",[role_id,user_id],(err,data)=>{
        if (err){
            console.log('给用户赋角色失败')
        }else{
            res.send({
                code:200,
                data:data
            })
        }
    })
}

getAllRightsTree = (req,res)=>{
    var sql = "select '101' role_id,t1.* from rights_info t1 ";
    dbConfig.sqlConnect(sql,[],(err,data)=>{
        if(err){
            console.log('获取权限树出错')
        }else{
            res.send({
                code:200,
                data:df(data)
            })
        }
    })
}

allowRights = (req,res)=>{
    let {ids,role_id} = req.body
    console.log(ids)
    console.log(role_id)
    var sql1 = "delete from role_right_rel where role_id=?"
    console.log(sql1)
    dbConfig.sqlConnect(sql1,[role_id],(err,data)=>{
        if(err)
            console.log('删除权限失败')
        else{
            var sql2 = "insert into role_right_rel(`role_id`,`rights_id`) values "
            var arr = ids.split(',')
            for(var i=0;i<arr.length;i++){
                if(i>0)
                    sql2 += ","
                sql2 += "('"+role_id+"','"+arr[i]+"')"
            }
            console.log(sql2)
            dbConfig.sqlConnect(sql2,[],(err,data)=>{
                if (err)
                    console.log('添加失败')
                else
                    res.send({
                        code:200,
                        data:data
                    })
            })
        }
    })

}

deleteRightsRel = (req,res)=>{
    var sql = "delete from role_right_rel where role_id=? and rights_id=?"
    let {role_id,rights_id} = req.query
    var sqlArr = [role_id,rights_id]
    dbConfig.sqlConnect(sql,sqlArr,(err,data)=>{
        if(err)
            console.log('删除用户权限出错')
        else{
            dbConfig.sqlConnect(" select t1.*,t3.* from role_info t1 left join role_right_rel t2 on t1.role_id = t2.role_id  left join rights_info t3 on t2.rights_id = t3.rights_id where t1.role_id=? order by t1.role_id,t3.rights_level,t3.prights_id",
                [role_id],(err,data)=>{
                    if(err)
                        console.log('获取新用户权限列表失败')
                    else
                        res.send({
                            code:200,
                            data:df(data)
                        })
                })
        }

    })
}
module.exports ={
    getRightsList,getRoleList,deleteRightsRel,getAllRightsTree,allowRights,allowUserRole
}
